<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'operating:fstAppointCase:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsOperatingFstAppointCaseForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { FstAppointCase, fstAppointCaseSave, fstAppointCaseForm } from '/@/api/operating/fstAppointCase';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('operating.fstAppointCase');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<FstAppointCase>({} as FstAppointCase);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增委案信息表') : t('编辑委案信息表'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('案件来源'),
      field: 'caseSource',
      component: 'Select',
      componentProps: {
        dictType: 'upstream',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('政策描述'),
      field: 'ps',
      component: 'Input',
    },
    {
      label: t('案件批次'),
      field: 'caseBatch',
      component: 'Input',
      required: true,
    },
    {
      label: t('委案表'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'fstAppointCase_file',
        uploadType: 'file',
        maxNumber: 1,
        accept: ['.xlsx', '.xls'],
      },
      rules: [
        { required: true },
        {
          validator(_rule, value) {
            return new Promise((resolve, reject) => {
              const len = !value || value['fstAppointCase_file__len'] || 0;
              if (len == 0) reject(t('请上传委案表'));
              else resolve();
            });
          },
        },
      ],
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await fstAppointCaseForm(data);
    record.value = (res.fstAppointCase || {}) as FstAppointCase;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await fstAppointCaseSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
